//
//  ControlsDataSource.m
//  iBasic
//
//  Created by Andrew Barry on 28/04/08.
//  Copyright 2008 Barry Computer Services. All rights reserved.
//

#import "ControlsDataSource.h"
#import "ControlRegistry.h"
#import "FormEditor.h"

#define MyPrivateTableViewDataType @"ControlsDataSource"

@implementation ControlsDataSource
-(void) awakeFromNib
{
	[list setDataSource: self];
	[self retain];
}

-(void)setForm: (FormEditor *) f {
	[form autorelease];
	form = [f retain];
	[list reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
	if (form && [form isWebForm])
		return 4;
	return [[ControlRegistry instance] count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
	return [[ControlRegistry instance] nameForIndex: rowIndex];
}

+(NSString *)controlTypeForIndex: (int) rowIndex {
	return [[ControlRegistry instance] basenameForIndex: rowIndex];
}

- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {
    // Copy the row numbers to the pasteboard.	
	int idx = [rowIndexes firstIndex];
	NSString *controlType = [ControlsDataSource controlTypeForIndex: idx];
	
    NSData *data = [controlType dataUsingEncoding:NSUTF8StringEncoding];
    [pboard declareTypes:[NSArray arrayWithObject:MyPrivateTableViewDataType] owner:self];
    [pboard setData:data forType:MyPrivateTableViewDataType];
    return YES;
}

@end
